package com.example.starter.work6

/**
@author whd
@date 2023/10/11 14:50:12
 */

import org.apache.rocketmq.client.producer.DefaultMQProducer
import org.apache.rocketmq.common.message.Message
import org.apache.rocketmq.remoting.common.RemotingHelper
import java.time.Duration
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter


var formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss:SSS")
@Throws(Exception::class)
fun main() {
  val producer = DefaultMQProducer("test-producer3", false)
  producer.namesrvAddr = "192.168.201.129:9876"
  producer.start()
    val msg = Message()
    msg.topic = "delay-topic3"
    msg.body = "这是第一条延迟消息".toByteArray(charset(RemotingHelper.DEFAULT_CHARSET))
    val messageDelayTime: Duration = Duration.ofSeconds(40)
    val delayTimestamp: Long = System.currentTimeMillis() + messageDelayTime.toMillis()
    // 绝对时间：定时消息
   // msg.deliverTimeMs = delayTimestamp
 // msg.setDelayTimeSec(1000*32);
 // producer.send(msg)
    val msg2 = Message()
    msg2.topic = "delay-topic3"
    msg2.body = "这是第二条延迟消息".toByteArray(charset(RemotingHelper.DEFAULT_CHARSET))
    val messageDelayTime2: Duration = Duration.ofSeconds(65)
    val delayTimestamp2: Long = System.currentTimeMillis() + messageDelayTime2.toMillis()
    // 绝对时间：定时消息
 //  msg2.deliverTimeMs = delayTimestamp2
    // 相对时间：延时消息

     //msg2.setDelayTimeSec(1000 * 67);

  producer.send(msg2,1000 * 67)
  producer.send(msg,1000*32)
    System.out.printf(msg.body.toString()+"发送时间：%s %n", formatter.format(LocalDateTime.now()))
    System.out.printf(msg2.body.toString()+"发送时间：%s %n", formatter.format(LocalDateTime.now()))

  producer.shutdown()
}


